#pragma once

#include "Common.h"
#include "Bool.h"
#include "BinomCoef.h"
#include "CzechMoveGen.h"

#define MAXIDX4 100
#define MAXIDX5 500
#define MAXIDX6 1000
#define MAXIDX7 31000
#define MAXIDX8 44000

#define MAXIDX4_DTC 100
#define MAXIDX5_DTC 500
#define MAXIDX6_DTC 1000
#define MAXIDX7_DTC 31000
#define MAXIDX8_DTC 44000


#define BLOCKNUM4 500
#define BLOCKNUM5 5500
#define BLOCKNUM6 75000
#define BLOCKNUM7 500000
#define BLOCKNUM8 4600000

#define BLOCKNUM4_DTC 4000
#define BLOCKNUM5_DTC 60000
#define BLOCKNUM6_DTC 800000
#define BLOCKNUM7_DTC 8000000
#define BLOCKNUM8_DTC 4600000


#define AUTOLOADSIZE 0

namespace DbReader
{

// definition of a structure for compressed databases
typedef struct compresseddatabase
	{
	int ispresent;			// does db exist?
	int numberofblocks;		// how many disk blocks does this db need?
	int blockoffset;		// offset to calculate unique block id for the blocks of this db
	int firstblock;			// where is the first block in it's db?
	int startbyte;			// at this byte
	int databasesize;		// index range for this database
	int value;				// WIN/LOSS/DRAW if single value, UNKNOWN == 0 else
	int *idx;				// pointer to an array of index numbers, array is 
							// allocated dynamically, to size n
	int fp;					// which file is it in?
	} cprsubdb;

// definition of a structure for compressed databases
typedef struct dtcdatabase
	{
	int ispresent;			// does db exist?
	int numberofblocks;		// how many disk blocks does this db need?
	int blockoffset;		// offset to calculate unique block id for the blocks of this db
	int firstblock;			// where is the first block in it's db?
	int startbyte;			// at this byte
	int databasesize;		// index range for this database
	int *idx;				// pointer to an array of index numbers, array is 
							// allocated dynamically, to size n
	int fp;					// which file is it in?
	} dtcdb;


void SetDatabaseReaderDirectory(const wchar_t* lpDir);
void SetReaderDatabaseName(wchar_t *dbname, int numPieces, bool index, bool dtc);
int ParseIndexFile(wchar_t* idxFilename, int blockoffset, int fpcount);
int ParseDtcIndexFile(wchar_t* idxFilename, int blockoffset, int fpcount);
int PreloadDatabase(SEngineData* ed);
int ReaderDbInit(int suggestedMB, SEngineData* ed);
void ReaderDbExit();

template<typename BB> int ReaderDbLookup(BB *q, int cl, uint8 *pDTC);
template<> int ReaderDbLookup<BitBoard>(BitBoard *q, int cl, uint8 *pDTC);
template<> int ReaderDbLookup<BitBoard64>(BitBoard64 *q, int cl, uint8 *pDTC);

}

